
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Live-Migration · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="dashboard/Dashboard.html" />
    
    
    <link rel="prev" href="FirstApplication.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    What is ThingsJS?
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="../about/Architecture.html">
            
                <a href="../about/Architecture.html">
            
                    
                    System Architecture
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="../about/Terminology.html">
            
                <a href="../about/Terminology.html">
            
                    
                    Terminology
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3" data-path="../api/CLI.html">
            
                <a href="../api/CLI.html">
            
                    
                    CLI Commands
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.4" data-path="../Publications.html">
            
                <a href="../Publications.html">
            
                    
                    Publications
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../GettingStarted.html">
            
                <a href="../GettingStarted.html">
            
                    
                    Getting Started
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="VirtualMachine.html">
            
                <a href="VirtualMachine.html">
            
                    
                    ThingsJS VM Image
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="Setup-PocketBeagle.html">
            
                <a href="Setup-PocketBeagle.html">
            
                    
                    Pocket Beagle
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="Setup-BeagleboneBlack.html">
            
                <a href="Setup-BeagleboneBlack.html">
            
                    
                    Beaglebone Black
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../samples/">
            
                <a href="../samples/">
            
                    
                    Sample Components
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../samples/RIoTBenchmarks.html">
            
                <a href="../samples/RIoTBenchmarks.html">
            
                    
                    Sample Benchmarks
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="./">
            
                <a href="./">
            
                    
                    Tutorials
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="FirstApplication.html">
            
                <a href="FirstApplication.html">
            
                    
                    Writing your First App
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.2" data-path="Migration.html">
            
                <a href="Migration.html">
            
                    
                    Live-Migration
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="dashboard/Dashboard.html">
            
                <a href="dashboard/Dashboard.html">
            
                    
                    Using the Dashboard
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../api/">
            
                <a href="../api/">
            
                    
                    Developer API
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../api/Devdocs.html">
            
                <a href="../api/Devdocs.html">
            
                    
                    Developer Documentation
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../api/Testing.html">
            
                <a href="../api/Testing.html">
            
                    
                    Running Unit Tests
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../api/Pubsub.html">
            
                <a href="../api/Pubsub.html">
            
                    
                    Pubsub
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../api/Code.html">
            
                <a href="../api/Code.html">
            
                    
                    Code
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../api/CodeEngine.html">
            
                <a href="../api/CodeEngine.html">
            
                    
                    CodeEngine
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../api/Dispatcher.html">
            
                <a href="../api/Dispatcher.html">
            
                    
                    Dispatcher
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../api/Scheduler.html">
            
                <a href="../api/Scheduler.html">
            
                    
                    Scheduler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../api/GFS.html">
            
                <a href="../api/GFS.html">
            
                    
                    File system
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../api/Dashboard.html">
            
                <a href="../api/Dashboard.html">
            
                    
                    Web Dashboard
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../Community/">
            
                <a href="../Community/">
            
                    
                    Community
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../Community/involve.html">
            
                <a href="../Community/involve.html">
            
                    
                    Get involved
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../Community/contribute.html">
            
                <a href="../Community/contribute.html">
            
                    
                    Contribute
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../Community/feedback.html">
            
                <a href="../Community/feedback.html">
            
                    
                    Feedback
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../Community/team.html">
            
                <a href="../Community/team.html">
            
                    
                    Team
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Live-Migration</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="live-migrating-a-javascript-process">Live-migrating a JavaScript process</h1>
<p>ThingsJS can be used to migrate a running Process from one Device to another in a stateful manner. For example, let&apos;s say we have a simple counter program with a variable <code>count</code> being incremented every second. After we run this program on Device A for 10 seconds, the variable <code>count</code> will have a value of <code>10</code>. If we then migrate this program to Device B, the program will resume from the same point, where <code>count</code> has the value <code>10</code>. In this tutorial, we will be performing live-migration of a simple Program, <code>factorial.js</code>, from one Worker to another.</p>
<h2 id="setuprequirement">Setup/Requirement</h2>
<ul>
<li>At least one machine, but preferably two machines to perform live-migration over the network</li>
<li>Code for <code>factorial.js</code></li>
</ul>
<h2 id="walk-through">Walk-through</h2>
<p>We will refer to the two devices involved in this tutorial as Device A and B. We will assume that Device A has IP address <code>192.168.0.100</code> and Device B has IP address <code>192.168.0.200</code>.</p>
<ol>
<li>First, we will need to set up the ThingsJS Workers. <strong>On both Device A and B</strong>, create a directory to use as the Worker&apos;s runtime environment.</li>
</ol>
<pre><code>~$ mkdir migration-tutorial
</code></pre><ol>
<li>Navigate into the new directory and create the Worker configuration files <strong>(for both Device A and B)</strong>.</li>
</ol>
<pre><code>~$ cd migration-tutorial
~/migration-tutorial$ vim my-worker.conf
</code></pre><ol>
<li>Configuration file should be a JSON string with the following properties: <code>pubsub_url</code>, <code>id</code>.</li>
</ol>
<pre><code>{
    &quot;pubsub_url&quot;: &quot;mqtt://192.168.0.100&quot;,
    &quot;id&quot;: &quot;worker-A&quot;
}
</code></pre><p>The value of <code>pubsub_url</code> should be a URL to the Publish/Subscribe service. In this tutorial, we will be hosting the Pub/Sub service on Device A, so we use the URL <code>mqtt://192.168.0.100</code> - replace this with the public IP of <em>your Device A</em>. <strong><code>pubsub_url</code> should have the same value</strong> on both Device A and B&apos;s configuration files, but <strong><code>id</code> should be assigned differently</strong>.</p>
<ol>
<li>Run the Publish/Subscribe service and verify that the workers connect okay.</li>
</ol>
<p>On Device A only:</p>
<pre><code>~$ things-js pubsub -d
</code></pre><p>Then on both devices:</p>
<pre><code>~/migration-tutorial$ things-js worker my-worker.conf
</code></pre><ol>
<li>On successful launch, you should see some messages on the console:</li>
</ol>
<pre><code>~/migration-tutorial$ things-js worker my-worker.conf
[Engine:worker-A] Initialized
[Engine:worker-A] connected to Pub/Sub at mqtt://192.168.0.100
</code></pre><p>If there is no message about connection to Pub/Sub, it is possible that the Pub/Sub service is not running, or that the configured IP address is not reachable.</p>
<ol>
<li>We will use the ThingsJS CLI to control the workers for this tutorial. On any machine (it does not have to be Device A or B, it just needs to be able to reach the Pub/Sub service), enter the following CLI command to run <code>factorial.js</code> on worker A. <strong>The working directory where the following command is entered must contain <code>factorial.js</code> source file.</strong></li>
</ol>
<pre><code>~$ things-js dispatch mqtt://192.168.0.100 run worker-A factorial.js
</code></pre><p><code>things-js dispatch</code> is a command for performing <code>Dispatcher</code> operations. It will create a temporary <code>Dispatcher</code> object, connect to the Pub/Sub service, execute <code>Dispatcher</code> actions, then terminate. Supported <code>Dispatcher</code> actions are: <code>run</code>, <code>pause</code>, <code>resume</code>, <code>kill</code>, <code>migrate</code>. The signature for a <code>things-js dispatch</code> command is thus:</p>
<pre><code>~$ things-js dispatch &lt;pubsub_url&gt; &lt;action&gt; [action_args...]
</code></pre><p>Once the above command is successfully run, the CLI should display an output like shown in the example below:</p>
<pre><code>{ engine: &apos;worker-A&apos;,
  id: &apos;HwJIiOTD&apos;,
  pid: 9398,
  status: &apos;Running&apos; }
</code></pre><p>The <code>id</code> field contains the ThingsJS Process ID (not the same as PID), which we need for controlling the <code>factorial.js</code> process. <strong>Take note of this value.</strong> (In the example it is <code>HwJIiOTD</code>)</p>
<p>On Device A&apos;s console, you should also see the output from <code>factorial.js</code>.</p>
<ol>
<li>Next we will migrate the running <code>factorial.js</code> process from <code>worker-A</code> to <code>worker-B</code>. To do this, enter the following command:</li>
</ol>
<pre><code>~$ things-js dispatch mqtt://192.168.0.100 migrate worker-A worker-B factorial.js HwJIiOTD
</code></pre><p>The last argument here is the ThingsJS Process ID we noted in step 6. Once the command is successful, you should see Device B continuing execution of <code>factorial.js</code>.</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="FirstApplication.html" class="navigation navigation-prev " aria-label="Previous page: Writing your First App">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="dashboard/Dashboard.html" class="navigation navigation-next " aria-label="Next page: Using the Dashboard">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Live-Migration","level":"1.3.2","depth":2,"next":{"title":"Using the Dashboard","level":"1.3.3","depth":2,"path":"tutorials/dashboard/Dashboard.md","ref":"tutorials/dashboard/Dashboard.md","articles":[]},"previous":{"title":"Writing your First App","level":"1.3.1","depth":2,"path":"tutorials/FirstApplication.md","ref":"tutorials/FirstApplication.md","articles":[]},"dir":"ltr"},"config":{"plugins":["jsdoc"],"root":"./guide","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"jsdoc":{"options":"{}"},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"gitbook":"*"},"file":{"path":"tutorials/Migration.md","mtime":"2018-10-15T23:34:02.479Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-04-24T23:23:12.622Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

